package ${package.Controller};

import io.aicn.lowcode.LowcodeEngineApplication;
import ${package.Controller}.${table.controllerName};
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import ${package.Entity}.vo.${entity}VO;
import ${package.Entity}.qo.${entity}QO;
import ${package.Entity}.qo.${entity}IdQO;
import ${package.Entity}.qo.${entity}BatchQO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuse.common.util.result.ResultJson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import java.util.TimeZone;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * <p>
 * ${table.comment!} 控制器单元测试
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LowcodeEngineApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class ${table.controllerName}Test  {

    protected static final Logger log = LoggerFactory.getLogger(${table.controllerName}Test.class);

    static {
        final TimeZone zone = TimeZone.getTimeZone("GMT+8"); // 获取中国时区
        TimeZone.setDefault(zone); // 设置时区
    }

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ${table.serviceName} ${table.entityPath}Service;

<#-- 获取主键字段信息 -->
    <#assign keyField = table.fields[0]>
    <#list table.fields as field>
        <#if field.keyFlag>
            <#assign keyField = field>
            <#break>
        </#if>
    </#list>

    /**
     * 测试分页查询${table.comment!}列表
     */
    @Test
    public void testGetPage() throws Exception {
        // 准备测试数据
        Page<${entity}> page = new Page<>(1, 10);
        List<${entity}> records = new ArrayList<>();
        ${entity} entity = new ${entity}();
        // TODO: 设置实体属性
        records.add(entity);
        page.setRecords(records);
        page.setTotal(1);

        // 配置Mock行为
        when(${table.entityPath}Service.page(any(Page.class), any(QueryWrapper.class))).thenReturn(page);

        // 执行测试并验证结果
        mockMvc.perform(post("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>/page")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"pageNum\": 1, \"pageSize\": 10}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.header.resultCode").value(200));
    }



    /**
     * 测试根据${keyField.comment!}查询${table.comment!}详情
     */
    @Test
    public void testGetBy${keyField.capitalName}() throws Exception {
        // 准备测试数据
        ${entity} entity = new ${entity}();
        // TODO: 设置实体属性

        // 配置Mock行为
        when(${table.entityPath}Service.getById(anyLong())).thenReturn(entity);

        // 执行测试并验证结果
        mockMvc.perform(post("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>/get")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"${keyField.propertyName}\": 1}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.success").value(true));
    }

    /**
     * 测试新增${table.comment!}
     */
    @Test
    public void testSave() throws Exception {
        // 配置Mock行为
        when(${table.entityPath}Service.save(any(${entity}.class))).thenReturn(true);

        // 执行测试并验证结果
        mockMvc.perform(post("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>/save")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.success").value(true));
    }

    /**
     * 测试根据${keyField.comment!}修改${table.comment!}
     */
    @Test
    public void testUpdateBy${keyField.capitalName}() throws Exception {
        // 准备测试数据
        ${entity} entity = new ${entity}();
        // TODO: 设置实体属性

        // 配置Mock行为
        when(${table.entityPath}Service.updateById(any(${entity}.class))).thenReturn(true);
        when(${table.entityPath}Service.getById(anyLong())).thenReturn(entity);

        // 执行测试并验证结果
        mockMvc.perform(post("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>/update")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"${keyField.propertyName}\": 1}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.success").value(true));
    }

    /**
     * 测试批量删除${table.comment!}
     */
    @Test
    public void testRemoveBy${keyField.capitalName}() throws Exception {
        // 配置Mock行为
        when(${table.entityPath}Service.removeByIds(anyList())).thenReturn(true);

        // 执行测试并验证结果
        mockMvc.perform(post("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${controllerMappingHyphen}<#else>${table.entityPath}</#if>/delete")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"ids\": [1, 2, 3]}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.success").value(true));
    }


}